﻿using HostRestartManager;

if (args.Length == 3)
{
    string application = args[0];
    string sourceFolder = args[1];
    string destFolder = args[2];
    string workingDirectory = args[2];

    int maxRetryCount = 10;
    int retryCount = 0;

    while (retryCount < maxRetryCount && Helpers.IsProcessRunning(application))
    {
        retryCount++;
        await Task.Delay(TimeSpan.FromSeconds(1));
        Console.WriteLine($"已重试 {retryCount} 次");
    }

    if (!Helpers.IsProcessRunning(application))
    {
        UpdateApplication(sourceFolder, destFolder, application);
        Startup(application, workingDirectory);
    }
    else
    {
        Console.WriteLine("重启失败");
    }
}

void UpdateApplication(string sourceFolder, string destFolder, string application)
{
    Helpers.CopyFiles(sourceFolder, destFolder, application);
}

void Startup(string application, string applicationDirectory)
{
    string command = "dotnet";
    string arguments = $"{application.Replace(".dll", "")}.dll";
    Helpers.ExecuteExternalProcess(command, arguments, applicationDirectory);
    Console.WriteLine("正在重启中......");
}